

export function debounce(callback,delay=200){ //防抖
    let timer=null;
    return function (){
        clearTimeout(timer) //关闭定时器
        timer=setTimeout(()=>{ //重新计时
            callback.apply(this)
            // console.log(this)  //this指向window
        },delay)
    }
}

export function throttle(callback,interval=300){//节流
        let flag=false;
        return function (){
            if(flag) return; //若进行中，则忽略本次触发
            flag=true;  //更改标志位
            setTimeout(()=>{
                callback.apply(this);
                flag=false    //执行完毕，还原标志位
            },interval)
         }
}

export function throttle2(callback,interval=300){//节流2
    let pre=0;
    return function (){
        let now=new Date();
        let context=this;
        let args=arguments;
        if(now-pre>interval){
                callback.apply(context,args);
                pre=now;
            }
        }
}